<script>
import { Message } from "element-ui";
export default {
  data() {
    return {
      loading: false,
      total: 50,
      courseMark: {
        pageNo: 1,
        pageSize: 5,
        userId: "",
        courseId: "",
        startTime: "",
        endTime: "",
      },
      tableData: [],
      courseMarkForm: {
        id:'',
        userId: "",
        courseId: "",
        content: "",
        grade: ""
      },

      title: "添加评论",
      dialogFormVisible: false,
      // 选择器列表
      courseList: [],
      userList:[],
      // 日期
      date:[]
    };
  },
  methods: {
    // 清除数据
    clear() {
      this.dialogFormVisible = false;
      this.courseMarkForm={
        id:'',
        userId: "",
        courseId: "",
        content: "",
        grade: 0
      }
    },
    //获取表格数据
    get() {
      this.loading = true;
      this.$http
        .get("/courseMark/list", {
          params: this.courseMark,
        })
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.tableData = res.data.data.list;
            this.total = res.data.data.total;
            this.loading = false;
          } else {
            Message({
              type: "error",
              message: "获取数据失败",
            });
          }
        })
        .catch((e) => {
          console.log(e.getMessage());
        });
    },
    // 获取课程名称
    getCourseList() {
      this.$http
        .get("/course/idAnName")
        .then((res) => {
          // console.log(res.data);
          if (res.data.status == 200) {
            this.courseList = res.data.data.list;
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    getUserList() {
      this.$http
        .get("/user/findAll")
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.userList = res.data.data;

          }
        })
        .catch((e) => {
          console.log(e);
        });
    },

    // 新增的表单
    addForm() {
      this.getCourseList()
      this.getUserList()
      this.clear()
      this.title = "添加评论"
      this.dialogFormVisible = true
    },
    // 添加
    add() {
      // let user = JSON.parse(sessionStorage.getItem("user"));
      // this.courseForm.teacherId = user.id;
      this.$http
        .post("/courseMark/add", this.courseMarkForm)
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            Message({
              message: "添加成功",
              type: "success",
              onClose:true
            });
            this.clear()
            this.get()
          } else {
            Message({
              message: res.data.data,
              type: "error",
            });
          }
        })
        .catch((e) => {});
    },

    editForm(item) {
      this.getCourseList()
      this.getUserList()
      this.title = "修改评论";
      this.dialogFormVisible = true;
      this.courseMarkForm = {
        id:item.id,
        userId: item.userId,
        courseId: item.courseId,
        content: item.content,
        grade: item.grade
      }
    },
    edit() {
      this.$http
        .post("/courseMark/update", this.courseMarkForm)
        .then((res) => {
          console.log(res.data);
          if (res.data.status == 200) {
            this.get()
            this.clear();
          } else {
            Message({
              message: res.data.data,
              type: "error",
            });
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },

    // 删除
    remove(item) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http
            .post("/courseMark/delete?id=" + item)
            .then((res) => {
              console.log(res.data);
              if (res.data.status == 200) {
                this.$message.success("");
                this.get();
              }
            })
            .catch((e) => {
              console.log(e);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //分页
    handleSizeChange(val) {
      this.courseMark.pageSize = val;
      this.get();
    },
    handleCurrentChange(val) {
      this.courseMark.pageNo = val;
      this.get();
    },
  },
  watch: {
    // 
    date(newVal) {
      if (newVal != null) {
        this.courseMark.startTime = newVal[0];
        this.courseMark.endTime = newVal[1];
      } else {
        this.courseMark.startTime=''
        this.courseMark.endTime=''
      }
      this.get()
    },
  },
  mounted() {
    this.get()
    this.getCourseList()
    this.getUserList()
  },
};
</script>

<template>
  <div>
    <!-- 搜索栏 -->
    <div class="head">
      <el-row>
        <span>课程评价</span>
        <!-- <el-input
          clearable
          @clear="get"
          style="width: 200px; margin-right: 10px"
          v-model="courseMark.userId"
          placeholder="请输入用户id"
        ></el-input> -->
        <el-select
              v-model="courseMark.userId"
              filterable
              clearable
              @clear="get"
              @change="get"
              placeholder="请选择用户"
              :click="userList"
              style="margin-right: 10px"
              ><el-option
                v-for="item in userList"
                :key="item.id"
                :label="item.nickname+'('+item.id+')'"
                :value="item.id"
              ></el-option>
            </el-select>
        <!-- <el-input
          clearable
          @clear="get"
          style="width: 200px;margin-right: 10px;"
          v-model="courseMark.courseId"
          placeholder="请输入课程id"
        ></el-input> -->
        <el-select
              v-model="courseMark.courseId"
              filterable
              clearable
              @clear="get"
              @change="get"
              placeholder="请选择课程"
              :click="courseList"
              ><el-option
                v-for="item in courseList"
                :key="item.id"
                :label="item.courseName"
                :value="item.id"
              ></el-option>
            </el-select>
        <span>创建时间:</span>
      <el-date-picker
        v-model="date"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        align="right"
        value-format="yyyy-MM-dd HH:mm:ss"
      >
      </el-date-picker>

        <el-button type="primary" @click="get">查找</el-button>
        <el-button type="primary" @click="addForm">新增</el-button>
        <!-- <el-button type="primary">管理</el-button> -->
      </el-row>

      <!-- 新增表单 -->
      <el-dialog
        :title="title"
        :visible.sync="dialogFormVisible"
        @close="clear"
      >
        <el-form :model="courseMarkForm">
          <el-form-item>
            <span>用户</span>
            <el-select
              v-model="courseMarkForm.userId"
              filterable
              clearable
              @clear="get"
              @change="get"
              placeholder="请选择用户"
              :click="userList"
              ><el-option
                v-for="item in userList"
                :key="item.id"
                :label="item.username"
                :value="item.id"
              ></el-option>
            </el-select>
            <span>课程</span>
            <el-select
              v-model="courseMarkForm.courseId"
              filterable
              placeholder="请选择"
              :click="courseList"
              ><el-option
                v-for="item in courseList"
                :key="item.id"
                :label="item.courseName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <span>评分</span>
            <el-rate v-model="courseMarkForm.grade"></el-rate>
          </el-form-item>
          <el-form-item>
            <span>评论</span>
            <el-input
              v-model="courseMarkForm.content"
              style="width: 400px"
            ></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="clear">取 消</el-button>
          <el-button type="primary" @click="add" v-show="title == '添加评论'"
            >添 加</el-button
          >
          <el-button type="primary" @click="edit" v-show="title == '修改评论'"
            >保 存</el-button
          >
        </div>
      </el-dialog>
    </div>

    <!-- 列表 -->
    <el-table
      :data="tableData"
      style="width: 100%"
      height="530"
      v-loading="loading"
      :header-cell-style="{ background: '#1890FF', color: 'white' }"
    >
      <el-table-column fixed prop="userId" label="用户" width="120">
      </el-table-column>
      <el-table-column fixed prop="courseName" label="课程" width="120">
      </el-table-column>
      <el-table-column fixed prop="content" label="评价内容" width="120">
      </el-table-column>
      <el-table-column fixed prop="createTime" label="评价时间" width="120">
      </el-table-column>
      <el-table-column fixed prop="grade" label="评价星级" width="270">
        <template slot-scope="scoped">
          <el-rate v-model="scoped.row.grade" disabled> </el-rate>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="success" @click="editForm(scope.row)"
            >编辑</el-button
          >
          <el-button type="danger" @click="remove(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="courseMark.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="courseMark.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 10px"
    >
    </el-pagination>
  </div>
</template>
<style>
span {
  margin: 2px 10px;
}
.head {
  margin: 10px 5px;
}
.el-button {
  margin: 5px 20px;
}
</style>